import BaseIndex from "../baseIndex";

export default class EntityEdit extends BaseIndex {
    constructor(props) {
        super(props);

        this.init();

        this.initExpand();
    }

    getEntityData(id, actionType, data) {
        const { name, primaryKey } = data.entityForm || data.entity;
        const { entityData, dataAction } = data;

        let expandMethods = data.entity.expandMethods;
        if (dataAction && dataAction.getEntityData) expandMethods = { getEntityData: dataAction.getEntityData };

        let serviceName = this.serviceName;
        let actionName = 'getEntityData';
        let pathQuery = '';

        let url = '';
        if (dataAction) {
            const { serviceInterface } = dataAction;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = name;
        }

        const payload = { action: this.getAction(id, actionType) };

        if (data.entityForm) {
            if (serviceName === "EntityFormService") {
                payload.EntityFormId = data.entityForm.entityFormId;
                payload.DataId = entityData.DataId;
            }
            else {
                payload.EntityData = { ...entityData };
                payload.EntityData.entityFormId = data.entityForm.entityFormId;
                payload.ServiceName = serviceName;
                payload.ActionName = actionName;
            }
        }
        else {
            const method = expandMethods && expandMethods.getEntityData ? "/" + expandMethods.getEntityData : "";
            const primaryKeyValue = entityData[primaryKey];
            pathQuery = `${method}(${primaryKeyValue})`;
        }

        if (entityData.queryString) pathQuery += "?" + entityData.queryString;

        if (url) payload.url = url + pathQuery;
        else if (pathQuery) payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    updateEntityData(id, actionType, data) {
        data.dataAction.updateServiceInterface = data.dataAction.serviceInterface;
        this.saveEntityData(id, actionType, data);
    }

    saveEntityData(id, actionType, data) {
        const { dataAction, entityData, pageData } = data;
        if (dataAction && dataAction.entityName) {
            const { entityName, primaryKey, insertMethodName, updateMethodName, defaultValues } = dataAction;
            data.entity = { name: entityName, primaryKey, defaultValues, expandMethods: { insert: insertMethodName, update: updateMethodName } };
        }

        const { name, primaryKey, expandMethods, defaultValues } = data.entityForm || data.entity;

        let primaryKeyValue = data.oldEntityData && data.oldEntityData[primaryKey] ? data.oldEntityData[primaryKey] : null;
        if (!primaryKeyValue && pageData[primaryKey]) primaryKeyValue = pageData[primaryKey];

        if (data.oldEntityData && data.oldEntityData.RowVersion) data.entityData.RowVersion = data.oldEntityData.RowVersion;

        if (defaultValues && defaultValues.length > 0) {
            defaultValues.forEach(d => {
                data.entityData[d.Name] = d.Value;
            });
        }

        let serviceName = this.serviceName;
        let actionName = primaryKeyValue ? "update" : "insert";
        if (data.entityForm) actionName = "saveEntityData";

        let url = '';
        let isUpdate = false;
        if (dataAction) {
            let serviceInterface;
            if (data.entityForm) {
                if (dataAction.serviceInterface) serviceInterface = dataAction.serviceInterface;
            }
            else {
                const { insertServiceInterface, updateServiceInterface } = dataAction;
                isUpdate = primaryKeyValue && updateServiceInterface;
                serviceInterface = isUpdate ? updateServiceInterface : insertServiceInterface;
            }
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = name;
        }

        const payload = { action: this.getAction(id, actionType) };

        if (data.entityForm) {
            if (serviceName === "EntityFormService") {
                payload.EntityFormId = data.entityForm.entityFormId;
                payload.EntityData = entityData;
            }
            else {
                payload.EntityData = { ...entityData };
                payload.EntityData.entityFormId = data.entityForm.entityFormId;
                payload.ServiceName = serviceName;
                payload.ActionName = actionName;
            }
        }
        else {
            let method = expandMethods && expandMethods.insert ? "/" + expandMethods.insert : "";
            if (primaryKeyValue) method = expandMethods && expandMethods.update ? "/" + expandMethods.update : "";

            let pathQuery = method;
            if (primaryKeyValue && isUpdate) {
                entityData[primaryKey] = primaryKeyValue;
                pathQuery = `${method}(${primaryKeyValue})`;
            }

            payload[name] = entityData;
            if (url) payload.url = url + pathQuery;
            else payload.pathQuery = pathQuery;
        }

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    deleteEntityData(id, actionType, data) {
        const { dataAction } = data;
        let entity = data.entity;

        const entityData = data.entityData || data.selectDataList[0];

        if (dataAction && dataAction.entityName) {
            const { entityName, primaryKey, deleteMethodName } = dataAction;
            entity = { name: entityName, primaryKey, expandMethods: { deleteMethodName } };
        }

        const primaryKeyValue = entityData[entity.primaryKey];
        const { RowVersion } = entityData;

        let serviceName = this.serviceName;
        let actionName = 'delete';

        let url = '';
        if (dataAction) {
            const { serviceInterface } = dataAction;
            serviceName = serviceInterface.serviceName;
            actionName = serviceInterface.actionName;
            if (serviceInterface.url === 'EntityService') url = entity.name;
        }

        const payload = { action: this.getAction(id, actionType) };

        if (data.entityForm) {
            if (serviceName === "EntityFormService") {
                payload.EntityFormId = data.entityForm.entityFormId;
                payload.EntityData = entityData;
            }
            else {
                payload.EntityData = { ...entityData };
                payload.EntityData.entityFormId = data.entityForm.entityFormId;
                payload.ServiceName = serviceName;
                payload.ActionName = actionName;
            }
        }

        const method = entity.expandMethods && entity.expandMethods.deleteMethodName ? "/" + entity.expandMethods.deleteMethodName : '';
        let pathQuery = `${method}(${primaryKeyValue})?RowVersion=${RowVersion || ''}`;
        if (entity.name === 'EntityFormData') {
            const { EntityFormId } = data.selectDataList[0];
            pathQuery += "&entityFormId=" + EntityFormId;
        }

        if (url) payload.url = url + pathQuery;
        else payload.pathQuery = pathQuery;

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    searchQuery(id, actionType, data) {
        const { url, QueryInfo, dataAction } = data;
        const payload = { action: this.getAction(id, actionType, false) };
        payload.action.dataName = data.entityName;
        payload.action.dataGridViewSearchQuery = data.dataGridViewSearchQuery;

        payload.url = url;
        payload.QueryInfo = QueryInfo;

        let serviceName = this.serviceName;
        let actionName = 'searchQuery';
        if (dataAction && dataAction.serviceInterface) {
            serviceName = dataAction.serviceInterface.serviceName;
            actionName = dataAction.serviceInterface.actionName;
        }

        this.dvaActions.dispatch(serviceName, actionName, payload);
    }

    setsearchQuery(id, actionType, data) {
        const dataName = data.action ? data.action.dataName : '';
        actionType = data.action.dataGridViewSearchQuery;
        data = this.setSearchQueryResponse(data, dataName);
        this.dispatchAction(id, actionType, data);
        return false;
    }
}
